package net.maxwoods.nimble.springmvc.config;

import com.sun.istack.internal.Nullable;
import net.maxwoods.nimble.springmvc.model.User;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;

public class MyHttpMessageConverter extends AbstractHttpMessageConverter<User> {

    @Override
    protected boolean supports(Class<?> clazz) {
        // 表明本HttpMessageConverter 只处理Date 这个类。
        return User.class.isAssignableFrom(clazz);
    }

    public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
        return super.canWrite(clazz, mediaType);
    }

    @Override
    protected User readInternal(Class<? extends User> clazz,
                                HttpInputMessage inputMessage) throws IOException,
            HttpMessageNotReadableException {
        return new User();
    }

    @Override
    protected void writeInternal(User obj,
                                 HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        String out = "hello: " + obj.toString();
        outputMessage.getBody().write(out.getBytes());
    }

}
